package com.ash.controller.user;

import com.ash.domain.User;
import com.ash.domain.UserInfo;
import com.ash.service.message.MessageService;
import com.ash.service.user.UserInfoService;
import com.ash.service.user.UserService;
import com.ash.util.MyFileUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;


/**
 * @author : Chen Chao
 * @encoding : UTF-8
 * @date : 2021/4/3 14:54
 * @file : project06.com.ash.controller.user.UserInfoController.java
 * @software : IntelliJ IDEA
 * @since : 1.8
 */
@Controller
public class UserInfoController {
    @Resource
    UserInfoService userInfoService;
    @Resource
    UserService userService;
    @Resource
    MessageService messageService;
    @RequestMapping("/user/userInfoUpdate")
    public String userInfoUpdate(HttpSession session) {
        return "user/userInfoUpdate";
    }

    @PostMapping("/user/updateInfo")
    @ResponseBody
    public Integer updateInfo(User user, UserInfo userInfo, HttpSession session) {
        int i = userService.update(user) + userInfoService.update(userInfo);
        if (i > 0) {
            session.setAttribute("user", userService.selectById(user.getUid()));
            session.setAttribute("userInfo", userInfo);
        }
        return i;
    }

    @RequestMapping("/login")
    public String login(Model mode, String uname, String password, String code, HttpSession session) {
        User user = new User();
        user.setPassword(password);
        user.setUname(uname);
        User user1 = userService.selectToLogin(user);
        /*获取session中的验证码*/
        String code1 = (String) session.getAttribute("code");
        if (user1 != null && code.equalsIgnoreCase(code1)) {
            session.setAttribute("user", user1);
            session.setAttribute("userInfo", userInfoService.selectById(user1.getUid()));
            session.setAttribute("messageCount",messageService.selectByToIdCount(user1.getUid(), (byte) 0));
            System.out.println("登录成功");
            return "main";
        } else {
            System.out.println("登录失败");
            System.out.println(code + "==" + code1);
            mode.addAttribute("loginError", "登录信息错误或验证码错误");
            return "forward:/jsp/login.jsp";
        }
    }
    @RequestMapping("/userInfo/upload")
    @ResponseBody
    public Integer  fileUpload2(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request){
        /*获取session中存储的数据*/
        User user =(User)request.getSession().getAttribute("user");
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("userInfo");
        /*上传文件*/
        File upload = MyFileUtil.upload(request.getSession().getServletContext().getRealPath("/img/user/" + user.getUname()), file);
        /*更新用户头像信息*/
        String absolutePath = upload.getAbsolutePath();
        userInfo.setUrl(absolutePath.substring(absolutePath.indexOf("img") - 1));
        userInfoService.update(userInfo);

        return 1;
    }
    @PostMapping("/register")
    @ResponseBody
    public Integer register(User user) {
        user.setRid(2);
        int i = userService.addToRegister(user);
        /*获取注册后的用户id并将用户id添加到对应的userInfo数据库中*/
        UserInfo userInfo = new UserInfo();
        userInfo.setUid(userService.selectToLogin(user).getUid());
        userInfoService.add(userInfo);
        return i;
    }
}

